package com.blb.exam.student.controller;

import com.blb.exam.common.controller.BaseServlet;
import com.blb.exam.common.entity.User;
import com.blb.exam.common.util.CookieUtils;
import com.blb.exam.common.util.MD5Utils;
import com.blb.exam.common.util.SpringUtils;
import com.blb.exam.student.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/user.do")
public class UserServlet extends BaseServlet {

    private UserService userService = SpringUtils.getBean(UserService.class);

    public void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        //获得参数
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String rememberMe = req.getParameter("rememberMe");
        //获得验证码参数
        String code = req.getParameter("code");
        //获得Session中的验证码
        String code2 = (String) req.getSession().getAttribute("code");
        if(code2 == null || !code2.equals(code)){
            //验证码错误
            req.setAttribute("msg","验证码错误");
            //跳转到登录页面
            req.getRequestDispatcher("login.jsp").forward(req,resp);
            return;
        }
        //登录验证
        User user = userService.login(username, MD5Utils.encrypt(password));
        if(user != null){
            //判断是否记住密码
            if("on".equals(rememberMe)){
                int age = 7 * 24 * 60 * 60;
                //保存密码
                CookieUtils.saveCookie(resp,"username",username,age);
                CookieUtils.saveCookie(resp,"password",password,age);
            }else{
                //删除密码
                CookieUtils.removeCookie(resp,"username");
                CookieUtils.removeCookie(resp,"password");
            }
            //将用户信息保存到Session中
            req.getSession().setAttribute("user",user);
            //跳转学生管理
            resp.sendRedirect("quetionSet.do?m=findAll");
        }else{
            //添加错误信息
            req.setAttribute("msg","账号或密码错误");
            //跳转到登录页面
            req.getRequestDispatcher("login.jsp").forward(req,resp);
        }
    }

    public void existsUsername(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获得参数
        String username = req.getParameter("username");
        boolean exists = userService.existsUsername(username);
        resp.getWriter().print(exists);
    }

    public void register(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获得参数
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String nickname = req.getParameter("nickname");
        String cellphone = req.getParameter("cellphone");
        password = MD5Utils.encrypt(password);
        User user = new User(0,username,password,nickname,cellphone,0);
        userService.register(user);
        resp.sendRedirect("login.jsp");
    }
}
